package id.co.rho.scoring.cid.client.mvc.model;

import id.co.rho.scoring.cid.client.mvc.AppView;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoader;
import com.extjs.gxt.ui.client.data.PagingModelMemoryProxy;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayoutData;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;

public class ModelSearchUIWeb implements  ModelSearchUI{
	
	private ContentPanel gridpanel;
	private FormPanel formPanel;
	private VBoxLayoutData vbData1;
	private VBoxLayoutData vbData2;
	private VBoxLayout vBoxLayout;
    private FieldSet fsSearch;
    private FormLayout formlayout;
	private Button searchButton;
	private Button resetButton;
	private TextField<String> fieldName;
	private TextField<String> fieldRevision;
	private List<ModelData> models = new ArrayList<ModelData>();
	private PagingModelMemoryProxy proxy;
	private PagingLoader<PagingLoadResult<ModelData>> loader;
	private ListStore<ModelData> listStore;
	private PagingToolBar toolBar;
	private List<ColumnConfig> configs;
	private ColumnModel columnModel;
	private Grid<ModelData> grid;
	private ContentPanel centerPanel;
	private ContentPanel content;
    private TextField<String> modelType;
    
	@Override
	public void initialize() {
		vbData1 = new VBoxLayoutData();
        vbData1.setFlex(0);
        vbData2 = new VBoxLayoutData();
        vbData2.setFlex(1);
        vBoxLayout = new VBoxLayout(VBoxLayout.VBoxLayoutAlign.STRETCH);	
        gridpanel = new ContentPanel();
        
        formlayout = new FormLayout();
        formlayout.setLabelAlign(FormPanel.LabelAlign.LEFT);
        
        fsSearch = new FieldSet();
        fsSearch.setHeading("Search Criteria");
        fsSearch.setLayout(formlayout);
        
        fieldName = new TextField<String>();
		fieldName.setName("fieldName");
		fieldName.setEmptyText("Input model name ..."); 
		fieldName.setFieldLabel("Model Name");
		fieldName.setStyleAttribute("padding-bottom", "5px");
		
		modelType = new TextField<String>();
		modelType.setName("modelType");
		modelType.setEmptyText("Input model type ..."); 
		modelType.setFieldLabel("Model Type");
		modelType.setStyleAttribute("padding-bottom", "5px");
		
		fieldRevision = new TextField<String>();
		fieldRevision.setName("fieldName");
		fieldRevision.setEmptyText("Input model revision ..."); 
		fieldRevision.setHideLabel(false);
		fieldRevision.setFieldLabel("Revision");
		fieldRevision.setStyleAttribute("padding-bottom", "5px");
		
		fsSearch.add(fieldName);
		fsSearch.add(modelType);
		fsSearch.add(fieldRevision);
		
        LayoutContainer lcButton = new LayoutContainer();
        lcButton.setStyleAttribute("margin-top", "10px");
        lcButton.setLayout(new HBoxLayout());
        fsSearch.add(lcButton);
        
        searchButton = new Button();
        searchButton.setStyleAttribute("padding-left", "5px");
        searchButton.setText("Search");
        lcButton.add(searchButton);
        
        resetButton = new Button();
        resetButton.setStyleAttribute("padding-left", "5px");
        resetButton.setText("Reset");
        lcButton.add(resetButton);

        formPanel = new FormPanel();
        formPanel.setHeading("Scoring Model Search");
        formPanel.setScrollMode(Style.Scroll.AUTO);
        formPanel.add(fsSearch);	
        this.generateGrid();
        content = new ContentPanel();
        
        content.setLayout(vBoxLayout);

        content.add(formPanel,vbData1);
        content.setScrollMode(Style.Scroll.AUTO);
        content.add(gridpanel,vbData2); 
        content.layout();	
        content.setHeaderVisible(false);
		
	}

	public void generateGrid() {
		proxy = new PagingModelMemoryProxy(this.models);  
	    loader = new BasePagingLoader<PagingLoadResult<ModelData>>(proxy);  
	    loader.setRemoteSort(true);  
	    
	    listStore = new ListStore<ModelData>(loader);

	    toolBar = new PagingToolBar(10);  
	    toolBar.bind(loader);  
	    loader.load(0, 10);
		configs = new ArrayList<ColumnConfig>();
		configs.add(new ColumnConfig("id","",0));
		configs.add(new ColumnConfig("name", "Name", 200));
		configs.add(new ColumnConfig("type", "Type", 200));
		configs.add(new ColumnConfig("revision", "Revision", 100));
		configs.add(new ColumnConfig("active", "Active", 100));
		columnModel = new ColumnModel(configs);
		columnModel.getColumn(0).setHidden(true);
	    grid = new Grid<ModelData>(listStore, columnModel);
	    

	    
        grid.setBorders(true);  
        grid.setAutoExpandColumn("name");  
        grid.getAriaSupport().setDescribedBy(toolBar.getId() + "-display");  
        gridpanel = new ContentPanel();
        gridpanel.setFrame(true);  
        gridpanel.setHeading("Scoring Model List");  
        gridpanel.setButtonAlign(HorizontalAlignment.CENTER);  
        gridpanel.setLayout(new FitLayout());  
        gridpanel.setBottomComponent(toolBar);  

        gridpanel.add(grid);
		
	}

	@Override
	public void show() {
		centerPanel = (ContentPanel) Registry.get(AppView.CENTER_PANEL);
		centerPanel.removeAll();
		centerPanel.setLayout(new FitLayout());
		centerPanel.add(content);
		centerPanel.layout();		
	}
	
	@Override
	public void clearData(){
		this.models = null;
		proxy.setData(this.models);
		listStore.getLoader().load();
		grid.getView().refresh(true);
		fieldName.clear();
		modelType.clear();
		fieldRevision.clear();
		vBoxLayout.layout();
		gridpanel.layout();
		centerPanel.layout();		
	}
	
	@Override
	public void setModels(List<ModelData> models) {
		this.models = models;
		proxy.setData(models);
		listStore.getLoader().load();
		grid.getView().refresh(true);
		vBoxLayout.layout();
		gridpanel.layout();
		content.layout();
		centerPanel.layout();		
	}

	@Override
	public Button getSearchButton() {
		return searchButton;
	}


	@Override
	public TextField<String> getFieldName() {
		return fieldName;
	}

	@Override
	public TextField<String> getFieldRevision() {
		return fieldRevision;
	}

	@Override
	public Grid<ModelData> getGrid() {
		return grid;
	}
	
	@Override
	public TextField<String> getModelType() {
		return modelType;
	}
	
	@Override
	public Button getResetButton() {
		return resetButton;
	}
	

}
